﻿using UnityEngine;

/// <summary>
/// Singleton pattern.
/// </summary>
public abstract class Singleton<T> : MonoBehaviour where T : Component
{

    [SerializeField]
    protected static T _instance;

    /// <summary>
    /// Singleton design pattern
    /// 单例模式
    /// </summary>
    /// <value>The instance.</value>
    public static T Instance
    {
        get
        {
            //if (!Application.isPlaying)
            //{
            //    return null;
            //}
            return _instance;
        }
    }

    /// <summary>
    /// On awake, we initialize our instance. Make sure to call base.Awake() in override if you need awake.
    /// </summary>
    protected void Awake()
    {
        // print(this+"virtual");
        if (!Application.isPlaying)
        {
            return;
        }

        _instance = this as T;
        FixedAwake();
    }
    /// <summary>
    /// 初始化组件。让子类也使用该方法
    /// </summary>
    public abstract void FixedAwake();
    
}
